// index.js
const app = getApp();

Page({
  data: {
    url: app.globalData.ossUrl,

    progressList: [],
    progressNow: '',

    piecesList: [{
      id: '0',
      isComplete: true,
      newGet: false
    },{
      id: '1',
      isComplete: true,
      newGet: false
    },{
      id: '2',
      isComplete: true,
      newGet: false
    },{
      id: '3',
      isComplete: true,
      newGet: false
    },{
      id: '4',
      isComplete: true,
      newGet: false
    },{
      id: '5',
      isComplete: true,
      newGet: false
    },{
      id: '6',
      isComplete: true,
      newGet: true
    },{
      id: '7',
      isComplete: false,
      newGet: false
    },{
      id: '8',
      isComplete: false,
      newGet: false
    },{
      id: '9',
      isComplete: false,
      newGet: false
    },{
      id: '10',
      isComplete: false,
      newGet: false
    },{
      id: '11',
      isComplete: false,
      newGet: false
    }],

    popShow: true,
    popStartShow: false,
    popMyShow: false,
    popAudioShow: false,
    popPosterShow: false,
    popRuleShow: false,
    popCompanyShow: false,
    popInfoShow: false,
    popTipsShow: true,

    myAudioList:[{
      id: 0,
      num: '1',
      txt: 'XXXXXXXXX',
      src: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/win.mp3',
      isPlaying: false
    },{
      id: 1,
      num: '2',
      txt: 'XXXXXXXXX',
      src: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/win.mp3',
      isPlaying: false
    },{
      id: 2,
      num: '3',
      txt: 'XXXXXXXXX',
      src: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/win.mp3',
      isPlaying: false
    },{
      id: 3,
      num: '4',
      txt: 'XXXXXXXXX',
      src: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/win.mp3',
      isPlaying: false
    },{
      id: 4,
      num: '5',
      txt: 'XXXXXXXXX',
      src: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/win.mp3',
      isPlaying: false
    },{
      id: 5,
      num: '6',
      txt: 'XXXXXXXXX',
      src: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/win.mp3',
      isPlaying: false
    }],

    popPosterList: [{
      id: 0,
      poster: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/pop_poster_img1.png',
      txt: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/pop_poster_txt1.png',
      text: '王一博现场拍摄',
      check: true
    },{
      id: 1,
      poster: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/pop_poster_img2.png',
      txt: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/pop_poster_txt2.png',
      text: '开启时间：05月08日',
      check: false,
      lock: true
    },{
      id: 2,
      poster: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/pop_poster_img2.png',
      txt: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/pop_poster_txt3.png',
      text: '开启时间：05月15日',
      check: false,
      lock: true
    },{
      id: 3,
      poster: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/pop_poster_img2.png',
      txt: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/pop_poster_txt4.png',
      text: '开启时间：05月22日',
      check: false,
      lock: true
    }],

    piecesVoiceShow: false,

    // 是否为首轮
    isFirstRound: true,
    // 是否已收集完成
    getAll: false,
    // 拼图合成完成
    puzzleComplete: false,
    // 显示礼盒动画
    boxShow: false,
    boxAniStep: 0,
    posterShow: false,

    popLastPosterShow: false,

    ribbonShow: false,

    firstEvent: false,
    secondEvent: false,
    thirdEvent: true,
    fourthEvent: false
  },

  popCloseTipsTap(e) {
    this.setData({
      popShow: false,
      popTipsShow: false
    })
  },

  popInfoCloseTap(e) {
    this.setData({
      popShow: false,
      popInfoCloseTap: false
    })
  },

  // 语音弹层 - 添加小妙
  popAudioTitleTap(e) {
    this.setData({
      popAudioShow: false,
      popCompanyShow: true,
    })
  },

  popPosterCloseTap(e) {
    wx.navigateTo({
      url: '../share/share',
    })
  },

  // 播放隐藏语音
  playVoiceTap(e) {
    const innerAudioContext = wx.createInnerAudioContext({
      useWebAudioImplement: true // 是否使用 WebAudio 作为底层音频驱动，默认关闭。对于短音频、播放频繁的音频建议开启此选项，开启后将获得更优的性能表现。由于开启此选项后也会带来一定的内存增长，因此对于长音频建议关闭此选项
    })
    innerAudioContext.src = 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/win.mp3'
    innerAudioContext.play() // 播放
    innerAudioContext.onEnded(function() {
      console.log('over!');
      innerAudioContext.destroy();
    })
  },

  // 保存壁纸
  savePosterTap(e) {
    wx.getImageInfo({
      src: '../../images/poster.jpg',
      success (res) {
        console.log(res)
        wx.saveImageToPhotosAlbum({
          filePath: res.path,
          success(res) { 
            console.log(res);
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              duration: 2000
            })
          },
          fail(res) {
            console.log(res);
          }
        })
      }
    })
    
  },

  // 合成按钮
  compoundTap(e) {
    let list = this.data.piecesList, getAll = this.data.getAll, that = this;
    if (getAll) {
      console.log('compete!!!');
      this.setData({
        puzzleComplete: true,
        boxShow: true
      })
      setTimeout(function() {
        that.setData({
          boxAniStep: 1,
        })
        setTimeout(function() {
          that.setData({
            boxAniStep: 2,
          })
          setTimeout(function() {
            that.setData({
              boxAniStep: 3,
              posterShow: true,
              ribbonShow: true
            })
            setTimeout(function() {
              that.setData({
                boxAniStep: 4,
              })
              setTimeout(function() {
                that.setData({
                  popShow: true,
                  popLastPosterShow: true
                })
              },1700);
            },500);
          },100);
        },800);
      },500);
    }else{
      this.setData({
        popShow: true,
        popStartShow: true,
      })
    }
  },

  // 查看更多活动信息
  myPosterTap(e) {
    this.setData({
      popShow: true,
      popPosterShow: true,
    })
  },

  // 开启一波留言板
  myAudioTap(e) {
    wx.navigateTo({
      url: '../board/board',
    })
  },

  // 海报弹窗列表点击
  popPosterTap(e) {
    let id = e.currentTarget.id, list = this.data.popPosterList, that = this;
    for (let i=0;i<list.length;i++) {
      if (id == list[i].id && list[i].check) {
        wx.navigateTo({
          url: '../share/share',
        })
      }
    }
  },

  audioListTap(e) {
    let id = e.currentTarget.id, list = this.data.myAudioList, that = this;
    for (let i=0;i<list.length;i++) {
      if (id == list[i].id) {
        const innerAudioContext = wx.createInnerAudioContext({
          useWebAudioImplement: true // 是否使用 WebAudio 作为底层音频驱动，默认关闭。对于短音频、播放频繁的音频建议开启此选项，开启后将获得更优的性能表现。由于开启此选项后也会带来一定的内存增长，因此对于长音频建议关闭此选项
        })
        innerAudioContext.src = list[i].src;
        innerAudioContext.play() // 播放
        list[i].isPlaying = true;
        that.setData({
          myAudioList: list
        })
        innerAudioContext.onEnded(function() {
          console.log('over!');
          innerAudioContext.destroy();
          list[i].isPlaying = false;
          that.setData({
            myAudioList: list
          })
        })
      }
    }
  },

  popMyPosterTap(e) {
    this.setData({
      popMyShow: false,
      popPosterShow: true,
    })
  },

  popMyAudioTap(e) {
    this.setData({
      popMyShow: false,
      popAudioShow: true,
    })
  },

  popCloseTap(e) {
    this.setData({
      popShow: false,
      popStartShow: false,
      popMyShow: false,
      popAudioShow: false,
      popPosterShow: false,
      popRuleShow: false,
      popCompanyShow: false,
    })
  },

  companyTap(e) {
    this.setData({
      popShow: true,
      popCompanyShow: true,
    })
  },

  ruleTap(e) {
    this.setData({
      popShow: true,
      popRuleShow: true,
    })
  },

  myTap(e) {
    this.setData({
      popShow: true,
      popMyShow: true,
    })
  },

  indexTap(e) {
    this.setData({
      popShow: true,
      popStartShow: true,
    })
  },

  pieceTap(e) {
    let id = e.currentTarget.id,list = this.data.piecesList, that = this;
    if (list[id].isComplete) {
      const innerAudioContext = wx.createInnerAudioContext({
        useWebAudioImplement: true // 是否使用 WebAudio 作为底层音频驱动，默认关闭。对于短音频、播放频繁的音频建议开启此选项，开启后将获得更优的性能表现。由于开启此选项后也会带来一定的内存增长，因此对于长音频建议关闭此选项
      })
      innerAudioContext.src = 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/win.mp3'
      innerAudioContext.play() // 播放
      that.setData({
        piecesVoiceShow: true
      })
      innerAudioContext.onEnded(function() {
        console.log('over!');
        innerAudioContext.destroy();
        that.setData({
          piecesVoiceShow: false
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let list = this.data.piecesList, num = 0, now = '', progressList = [];
    list.forEach(e => {
      if (e.isComplete) num ++;
    });
    num<10 ?now = '0'+num :now = num;
    if (num == list.length) {
      this.setData({
        getAll: true
      })
    }
    for (let i=0;i<list.length;i++){
      i < num ?progressList.push(true) :progressList.push(false);
    }
    this.setData({
      progressNow: now,
      progressList
    })
  }
})
